﻿using Autofac;
using Common;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DBContext;
using EV.EventBus;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ZoneLine.Common;
using ZoneLine.Models;
using ZoneLine.Models.PrintModeConfig;
using ZoneLine.Models.StationConfig;
using ZoneLine.Printers;
using ZoneLine.Stations;
using ZoneLine.Stations.QOPStation;

namespace ZoneLine.ViewModels
{
    public class PackingMainViewModel : ObservableObject
    {
        private IModelDAL _dal;

        public PackingMainViewModel(IModelDAL dal, QOP006Station qOP006Station, QOP010Station qOP010Station, QOP020Station qOP020Station, QOP050LStation qOP050LStation, QOP050RStation qOP050RStation, QOP060Station qOP060Station)
        {
            _dal = dal;

            try
            {
                SysCfg.ReloadPrintConfig();
            }
            catch (Exception ex)
            {
                //todo 加载打印配置失败
            }
        }

        #region 装箱

        private PackageBoxViewModel _packageBoxView;

        public PackageBoxViewModel PackageBoxView
        {
            get { return _packageBoxView; }
            set => SetProperty(ref _packageBoxView, value);
        }
        #endregion 装箱

        /*
        #region 打印配置

        private bool _enterAuth;

        public bool EnterAuth
        {
            get { return _enterAuth; }
            set => SetProperty(ref _enterAuth, value);
        }

        private RelayCommand _savePrintConfigCmd;

        public RelayCommand SaveConfigCmd
        {
            get { return _savePrintConfigCmd ?? (_savePrintConfigCmd = new RelayCommand(SavePrintConfig)); }
        }

        private void SavePrintConfig()
        {
            var config = ContainerServer.Container.Resolve<PrintConfigTotal>();
            try
            {
                _dal.UpdateModelSuper(p => true, config.LxqcConfig);
            }
            catch (Exception ex)
            {
                MessageBox.Show("理想汽车打印配置保存失败" + ex.Message);
            }

            try
            {
                _dal.UpdateModelSuper(p => true, config.Sqqc_Config);
            }
            catch (Exception ex)
            {
                MessageBox.Show("上汽汽车打印配置保存失败" + ex.Message);
            }

            try
            {
                _dal.UpdateModelSuper(p => true, config.BqqcLHConfig);
            }
            catch (Exception ex)
            {
                MessageBox.Show("北汽汽车螺簧打印配置保存失败" + ex.Message);
            }

            try
            {
                _dal.UpdateModelSuper(p => true, config.BqqcKhConfig);
            }
            catch (Exception ex)
            {
                MessageBox.Show("北汽汽车空簧打印配置保存失败" + ex.Message);
            }

            try
            {
                _dal.UpdateModelSuper(p => true, config.WlqcConfig);
            }
            catch (Exception ex)
            {
                MessageBox.Show("蔚来汽车打印配置保存失败" + ex.Message);
            }

            try
            {
                _dal.UpdateModelSuper(p => true, config.PackageConfigModel);
            }
            catch (Exception ex)
            {
                MessageBox.Show("打包打印配置保存失败" + ex.Message);
            }
        }

        public RelayCommand ReloadConfigCmd => new RelayCommand(() =>
        {
            SysCfg.ReloadPrintConfig();
        });

     
        private RelayCommand<string> _selectTemplateCmd;

        public RelayCommand<string> SelectTemplateCmd
        {
            get { return _selectTemplateCmd ?? (_selectTemplateCmd = new RelayCommand<string>(SelectTemplate)); }
        }

        private void SelectTemplate(string info)
        {
            FileDialog dia = new OpenFileDialog();
            var rlt = dia.ShowDialog();
            string path = string.Empty;
            if (rlt.HasValue && rlt.Value)
            {
                path = dia.FileName;
            }
            else
            {
                return;
            }
            var config = ContainerServer.Container.Resolve<PrintConfigTotal>();
            switch (info)
            {
                case "LX":
                    config.LxqcConfig.TemplatePath = path;
                    break;

                case "SQ":
                    config.Sqqc_Config.TemplatePath = path;
                    break;

                case "BQLH":
                    config.BqqcLHConfig.TemplatePath = path;
                    break;

                case "BQKH":
                    config.BqqcKhConfig.TemplatePath = path;
                    break;

                case "WL":
                    config.WlqcConfig.TemplatePath = path;
                    break;

                default:
                    break;
            }
        }

        private int _startPrintIndex = 1;

        public int StartPrintIndex
        {
            get { return _startPrintIndex; }
            set => SetProperty(ref _startPrintIndex, value);
        }

        private int _endPrintIndex = 5;

        public int EndPrintIndex
        {
            get { return _endPrintIndex; }
            set => SetProperty(ref _endPrintIndex, value);
        }

        private RelayCommand<string> _printTestCmd;

        public RelayCommand<string> PrintTestCmd
        {
            get { return _printTestCmd ?? (_printTestCmd = new RelayCommand<string>(PrintTest)); }
        }

        private void PrintTest(string s)
        {
            try
            {
                var config = _dal.GetModel<QOP050LStationConfig>(p => true);
                var printConfig = ContainerServer.Container.Resolve<PrintConfigTotal>();
                switch (s)
                {
                    case "LX":
                        var printerLx = new LXQCPrinter();
                        printerLx.PrintLabel(printConfig.LxqcConfig.TemplatePath, config.PrinterName, StartPrintIndex);
                        break;

                    case "SQ":
                        var printerSq = new SQQCPrinter();
                        printerSq.PrintLabel(printConfig.Sqqc_Config.TemplatePath, config.PrinterName, StartPrintIndex, EndPrintIndex);
                        break;

                    case "BQLH":
                        var printerBqLh = new BQLHPrinter();
                        printerBqLh.PrintLabel(printConfig.BqqcLHConfig.TemplatePath, config.PrinterName, StartPrintIndex);
                        break;

                    case "BQKH":
                        var printerBqKh = new BQKHPrinter();
                        printerBqKh.PrintLabel(printConfig.BqqcKhConfig.TemplatePath, config.PrinterName, StartPrintIndex);
                        break;

                    case "WL":
                        var printerWl = new WLQCPrinter();
                        printerWl.PrintLabel(printConfig.WlqcConfig.TemplatePath, config.PrinterName, StartPrintIndex);
                        break;

                    case "LX_Show":
                        Task.Run(() =>
                        {
                            var printerLx_show = new LXQCPrinter();
                            printerLx_show.PrintLabelShow(printConfig.LxqcConfig.TemplatePath, config.PrinterName, StartPrintIndex);
                        });
                        break;

                    case "SQ_Show":
                        Task.Run(() =>
                        {
                            var printerSq_show = new SQQCPrinter();
                            printerSq_show.PrintLabelShow(printConfig.Sqqc_Config.TemplatePath, config.PrinterName, StartPrintIndex);
                        });
                        break;

                    case "BQLH_Show":
                        Task.Run(() =>
                        {
                            var printerBqLh_show = new BQLHPrinter();
                            printerBqLh_show.PrintLabelShow(printConfig.BqqcLHConfig.TemplatePath, config.PrinterName, StartPrintIndex);
                        });
                        break;

                    case "BQKH_Show":
                        var printerBqKh_show = new BQKHPrinter();
                        printerBqKh_show.PrintLabelShow(printConfig.BqqcKhConfig.TemplatePath, config.PrinterName, StartPrintIndex);
                        break;

                    case "WL_Show":
                        var printerWl_show = new WLQCPrinter();
                        printerWl_show.PrintLabelShow(printConfig.WlqcConfig.TemplatePath, config.PrinterName, StartPrintIndex);
                        break;

                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion 打印配置
        */
    }
}